package cn.iocoder.yudao.module.cmt.controller.admin.activity;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.cmt.controller.admin.activity.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.activity.ActivityInfoDO;
import cn.iocoder.yudao.module.cmt.service.activity.ActivityInfoService;

@Tag(name = "管理后台 - 活动信息")
@RestController
@RequestMapping("/cmt/activity-info")
@Validated
public class ActivityInfoController {

    @Resource
    private ActivityInfoService activityInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建活动信息")
    @PreAuthorize("@ss.hasPermission('cmt:activity-info:create')")
    public CommonResult<Long> createActivityInfo(@Valid @RequestBody ActivityInfoSaveReqVO createReqVO) {
        return success(activityInfoService.createActivityInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新活动信息")
    @PreAuthorize("@ss.hasPermission('cmt:activity-info:update')")
    public CommonResult<Boolean> updateActivityInfo(@Valid @RequestBody ActivityInfoSaveReqVO updateReqVO) {
        activityInfoService.updateActivityInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除活动信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:activity-info:delete')")
    public CommonResult<Boolean> deleteActivityInfo(@RequestParam("id") Long id) {
        activityInfoService.deleteActivityInfo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除活动信息")
    @PreAuthorize("@ss.hasPermission('cmt:activity-info:delete')")
    public CommonResult<Boolean> deleteActivityInfoList(@RequestParam("ids") List<Long> ids) {
        activityInfoService.deleteActivityInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得活动信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:activity-info:query')")
    public CommonResult<ActivityInfoRespVO> getActivityInfo(@RequestParam("id") Long id) {
        ActivityInfoDO activityInfo = activityInfoService.getActivityInfo(id);
        return success(BeanUtils.toBean(activityInfo, ActivityInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得活动信息分页")
    @PreAuthorize("@ss.hasPermission('cmt:activity-info:query')")
    public CommonResult<PageResult<ActivityInfoRespVO>> getActivityInfoPage(@Valid ActivityInfoPageReqVO pageReqVO) {
        PageResult<ActivityInfoDO> pageResult = activityInfoService.getActivityInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ActivityInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出活动信息 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:activity-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportActivityInfoExcel(@Valid ActivityInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ActivityInfoDO> list = activityInfoService.getActivityInfoPage(pageReqVO).getList();
        ExcelUtils.write(response, "活动信息.xls", "数据", ActivityInfoRespVO.class,
                        BeanUtils.toBean(list, ActivityInfoRespVO.class));
    }

}
